


//
//  VisitView.swift
//  Sina
//
//  Created by 管观荣 on 15/9/21.
//  Copyright © 2015年 CHer. All rights reserved.
//

import UIKit

class VisitView: UIView {

    //设置视图显示
     func setupView(imageName:String?,title:String){
        
        descLable.text = title
        
        if imageName != nil {
            iconImage.image = UIImage(named: imageName!)
            homeImage.hidden = true
            //移到后面
            sendSubviewToBack(maskImage)
        }else{//首页设置动画
            setupAnim()
        }
    }
    
    private func setupAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 5
        // 通常应用在循环播放的动画，动画会绑定在图层上，不会被销毁
        // 提示：当视图被销毁时，动画会一起被销毁，不需要考虑额外的操作
        anim.removedOnCompletion = false
        //添加到图片
        iconImage.layer.addAnimation(anim, forKey: "transform.rotation")
    }
    //MARK:- 界面
    //初始化  构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //从storyboard和Xib加载
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    private func setupUI(){
        //添加子控件
        addSubview(iconImage)
        addSubview(homeImage)
        addSubview(maskImage)
        addSubview(descLable)
        addSubview(registBtn)
        addSubview(loginBtn)
        //设置子控件的translatesAutoresizingMaskIntoConstraints
        for view in subviews{
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        //添加约束
        // 1> 图标
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: .CenterX, relatedBy: .Equal, toItem: iconImage, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: .CenterY, relatedBy: .Equal, toItem: iconImage, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 3> 消息文字
        addConstraint(NSLayoutConstraint(item: descLable, attribute: .CenterX, relatedBy: .Equal, toItem: iconImage, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: descLable, attribute: .Top, relatedBy: .Equal, toItem: iconImage, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: descLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: descLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Left, relatedBy: .Equal, toItem: descLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Top, relatedBy: .Equal, toItem: descLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: descLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: descLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
      addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskImage]))
      addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -70], views: ["mask": maskImage, "regButton": registBtn]))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    //MARK:- 懒加载控件
    //图标
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named:  "visitordiscover_feed_image_smallicon"))
        
    // 遮盖
    private lazy var maskImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
   //房子图标
    private lazy var homeImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
     //文字
    private lazy var descLable:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    //注册
     lazy var registBtn:UIButton = {
        let registBtn = UIButton()
        registBtn.setTitle("注册", forState: UIControlState.Normal)
        registBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return registBtn
    }()
    //登录
     lazy var loginBtn:UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return loginBtn
    }()
}
